/*
 * 人人信用管理有限公司版权所有.
 * 
 * @Description:
 * 
 * @author: liyuliang
 * 
 * @date: 20 Apr 2017 10:44:16 am
 * 
 * 注意：本内容仅限于人人信用管理有限公司内部传阅，禁止外泄以及用于其他的商业目的.
 */

package com.rrx360.common.web.spring;

import java.beans.PropertyEditorSupport;
import java.sql.Timestamp;
import java.util.Date;

import org.springframework.util.StringUtils;

import com.rrx360.common.util.DateUtil;

public class RxCustomDateEditor extends PropertyEditorSupport {

  private final boolean allowEmpty;

  private final int exactDateLength;

  private final Class type;


  /**
   * Create a new CustomDateEditor instance, using the given DateFormat for parsing and rendering.
   * <p>
   * The "allowEmpty" parameter states if an empty String should be allowed for parsing, i.e. get
   * interpreted as null value. Otherwise, an IllegalArgumentException gets thrown in that case.
   * 
   * @param dateFormat DateFormat to use for parsing and rendering
   * @param allowEmpty if empty strings should be allowed
   */

  public RxCustomDateEditor(boolean allowEmpty) {
    this.allowEmpty = allowEmpty;
    this.exactDateLength = -1;
    this.type = Date.class;
  }

  public RxCustomDateEditor(Class type, boolean allowEmpty) {
    this.allowEmpty = allowEmpty;
    this.exactDateLength = -1;
    this.type = type;
  }

  /**
   * Create a new CustomDateEditor instance, using the given DateFormat for parsing and rendering.
   * <p>
   * The "allowEmpty" parameter states if an empty String should be allowed for parsing, i.e. get
   * interpreted as null value. Otherwise, an IllegalArgumentException gets thrown in that case.
   * <p>
   * The "exactDateLength" parameter states that IllegalArgumentException gets thrown if the String
   * does not exactly match the length specified. This is useful because SimpleDateFormat does not
   * enforce strict parsing of the year part, not even with {@code setLenient(false)}. Without an
   * "exactDateLength" specified, the "01/01/05" would get parsed to "01/01/0005". However, even
   * with an "exactDateLength" specified, prepended zeros in the day or month part may still allow
   * for a shorter year part, so consider this as just one more assertion that gets you closer to
   * the intended date format.
   * 
   * @param dateFormat DateFormat to use for parsing and rendering
   * @param allowEmpty if empty strings should be allowed
   * @param exactDateLength the exact expected length of the date String
   */
  public RxCustomDateEditor(Class type, boolean allowEmpty, int exactDateLength) {
    this.allowEmpty = allowEmpty;
    this.exactDateLength = exactDateLength;
    this.type = type;
  }


  /**
   * Parse the Date from the given text, using the specified DateFormat.
   */
  @Override
  public void setAsText(String text) throws IllegalArgumentException {
    // 为空时的处理逻辑
    if (text == null || !StringUtils.hasText(text)) {
      if (this.allowEmpty) {
        setValue(null);
      } else {
        throw new IllegalArgumentException("Could not parse date: " + text);
      }
      // 非空时的处理逻辑
    } else {
      if (this.exactDateLength >= 0 && text.length() != this.exactDateLength) {
        throw new IllegalArgumentException(
            "Could not parse date: it is not exactly" + this.exactDateLength + "characters long");
      } else {
        if (!text.contains(":") && text.length() == 10) {
          text += " 00:00:00";
        }
        Date date = DateUtil.parseYyyy_MM_ddHHmmss(text);
        if (java.sql.Date.class == type) {
          setValue(new java.sql.Date(date.getTime()));
        } else if (Timestamp.class == type) {
          setValue(new Timestamp(date.getTime()));
        } else {
          setValue(date);
        }
      }
    }
  }

  /**
   * Format the Date as String, using the specified DateFormat.
   */
  @Override
  public String getAsText() {
    Date value = (Date) getValue();
    return (value != null ? DateUtil.formatYyyy_MM_dd_HHmmss(value) : "");
  }

}
